//
//  JMAuthenticationInputListItemView.m
//  NewiOSProject
//
//  Created by li on 2023/9/5.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMAuthenticationInputListItemView.h"
#import "CustomStackView.h"
#import "PaddingView.h"
#import "UIView+PopupProtocol.h"
#import "JMPickerSelectionView.h"
@interface JMAuthenticationInputListItemView ()<UITextFieldDelegate>

@property(nonatomic, strong) CustomStackView *stackView;
@property(nonatomic, strong, nonnull) PaddingView *rootView;

@property(nonatomic, assign) BOOL isLoading;
@end

@implementation JMAuthenticationInputListItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.canClick = YES;
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    
    self.rootView = [PaddingView initWithContentView:self.stackView edgeInsets:(UIEdgeInsetsMake(0, 20, 0, 20))];
//    self.rootView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:self.rootView];
    self.rootView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.rootView.topAnchor constraintEqualToAnchor:self.topAnchor],
        [self.rootView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
        [self.rootView.widthAnchor constraintEqualToAnchor:self.widthAnchor],
        [self.rootView.heightAnchor constraintEqualToAnchor:self.heightAnchor],
    ]];

    
    
    [self.stackView addArrangedSubview:self.titleLab];
    
    CustomStackView *inputStack = [CustomStackView init:(UILayoutConstraintAxisHorizontal)];
    self.inputStack = inputStack;
    inputStack.spacing = 10;
   
    [inputStack addArrangedSubview: self.textField];
    [inputStack addArrangedSubview:self.moreImage];
    [self.stackView addArrangedSubview: [[PaddingView initWithContentView:inputStack edgeInsets:(UIEdgeInsetsMake(0, 0, 0, 0))] then:^(PaddingView * _Nonnull view) {
        
        [[UIApplication sharedApplication].keyWindow resignFirstResponder];
        view.layer.cornerRadius = 12;
        view.layer.masksToBounds = YES;
//        view.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
        [view.heightAnchor constraintEqualToConstant:44].active = YES;
    }]];
    
    CustomStackView *lineStack = [CustomStackView init:(UILayoutConstraintAxisHorizontal)];
    [self.stackView addArrangedSubview: [[PaddingView initWithContentView:lineStack edgeInsets:(UIEdgeInsetsMake(0, 0, 0, 0))] then:^(PaddingView * _Nonnull view) {
        view.backgroundColor = mainSepartorColor();
        [view.heightAnchor constraintEqualToConstant:1].active = YES;
    }]];
   
    [self.stackView addArrangedSubview: [[PaddingView initWithContentView:self.warningLab edgeInsets:(UIEdgeInsetsMake(0, 0, 0, 0))] then:^(PaddingView * _Nonnull view) {
        
    }]];
    self.warningLab.superview.hidden = YES;
    
    WeakSelf
    [inputStack addTapGestuer:^(UIView * _Nonnull view) {
        if (weakSelf.canClick == NO) {
            return;
        }
        if (weakSelf.willShowPopupView) {
            weakSelf.willShowPopupView();
        }
        [weakSelf.popupView popup:[UIApplication sharedApplication].keyWindow edgeInsets:(UIEdgeInsetsZero)];
        [_popupView setRoundedCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) radii:30];
    }];
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    if (self.willInputCallback) {
        self.willInputCallback();
    }
}
- (void)textFieldDidChangeValue:(NSNotification *)notification
{
    UITextField *sender = (UITextField *)[notification object];

    if (self.inputCallback) {
        self.inputCallback(sender.text);
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (self.inputCallback) {
        self.inputCallback(textField.text);
    }
}

//- (void)refreshUI {
//    if (self.model == nil) {
//        return;
//    }
//    
//    self.titleLab.text = self.model.title;
//    self.textField.text = self.model.input;
//    self.textField.userInteractionEnabled = self.model.allowInput;
//    
//}

- (CustomStackView *)stackView {
    if (!_stackView) {
        self.stackView = [CustomStackView init:(UILayoutConstraintAxisVertical)];
        _stackView.spacing = 6;
        
    }
    return _stackView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        self.titleLab = [UILabel new];
        _titleLab.font = [UIFont systemFontOfSize:14];
        _titleLab.textColor = [UIColor colorWithHexString:@"#0F1511"];
        _titleLab.numberOfLines = 0;
    }
    return _titleLab;
}

- (UITextField *)textField {
    if (!_textField) {
        self.textField = [UITextField new];
        _textField.textColor = [UIColor colorWithHexString: @"#0F1511"];
        _textField.font = [UIFont systemFontOfSize:16];
        _textField.delegate = self;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChangeValue:) name:UITextFieldTextDidChangeNotification object:_textField];

//        [_textField.heightAnchor constraintEqualToConstant:40].active = YES;
//        [_textField.widthAnchor constraintEqualToConstant:300].active = YES;
    }
    return _textField;
}

- (UIImageView *)moreImage {
    if (!_moreImage) {
        self.moreImage = [UIImageView new];
        _moreImage.image = [UIImage imageNamed:@"more_down.png"];
        _moreImage.contentMode = UIViewContentModeScaleAspectFit;
        [_moreImage.widthAnchor constraintEqualToConstant:12].active = YES;
    }
    return _moreImage;
}

- (JMPickerSelectionView *)popupView {
    if (!_popupView) {
        self.popupView = [JMPickerSelectionView new];
        [_popupView.widthAnchor constraintEqualToConstant: kScreenWidth].active = YES;
//        [_popupView.heightAnchor constraintEqualToConstant: 500].active = YES;
        _popupView.backgroundColor = [UIColor whiteColor];
    }
    return _popupView;
}

- (UILabel *)warningLab {
    if (!_warningLab) {
        self.warningLab = [UILabel new];
        _warningLab.textColor = [UIColor colorWithHexString:@"#FF3F33"];
        _warningLab.font = [UIFont systemFontOfSize:12];
        _warningLab.numberOfLines = 0;
    }
    return _warningLab;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
